package com.flin.icesilkworm.examination.bas.populator;

import java.util.Optional;

import com.flin.icesilkworm.examination.parent.IExamParent.IExamParentConstant;
import com.flin.icesilkworm.examination.parent.IExamParent.IExamParentExample;
import com.flin.icesilkworm.framework.aop.IFilterExpByService;
import com.flin.icesilkworm.framework.jdbc.interceptors.JdbcExampleSupport;
import com.flin.icesilkworm.framework.spring.SpringWebUtils;
@SuppressWarnings({"unchecked","rawtypes"})
public class PopulatorSupport implements IFilterExpByService{

	public static <T extends JdbcExampleSupport<?>> T setDefaultAuthCondition(IExamParentExample<T> filter) {
		Optional.ofNullable(SpringWebUtils.getUserIdByRole("3")).ifPresent(userId->
			filter.andBankAuthEqualTo(IExamParentConstant.BANK_AUTH_PUBLIC).or(true, t->{
					((IExamParentExample<IExamParentExample>)t)
						.andBankAuthEqualTo(IExamParentConstant.BANK_AUTH_PRIVATE)
						.andTeacherIdEqualTo(userId);
				})
		);
		return (T)filter;
	}

	public <T extends JdbcExampleSupport<?>> T filter(T t) {
		if(t instanceof IExamParentExample){
			filter(t);
		}
		return t;
	}
}
